import axios from 'axios'
import { ElMessage, ElNotification } from 'element-plus'

const BAIDU_MAP_AK = 'Ai0WGVDpQy6Nku83k3BuP2B9rY51aNVt'

export function useLocationSearch() {
    const queryLocationSuggestions = async (queryString, cb) => {
        if (!queryString) {
            cb([])
            return
        }

        try {
            const url = `/map/place/v2/suggestion?query=${encodeURIComponent(queryString)}&output=json&ak=${BAIDU_MAP_AK}&region=无&city_limit=false`
            const response = await axios.get(url)

            const suggestions = response.data.result
                .map(item => {
                    if (item.location && item.address) {
                        return {
                            value: item.name,
                            address: item.address,
                            location: item.location,
                        }
                    }
                    return null
                })
                .filter(Boolean)

            cb(suggestions)
        } catch (error) {
            console.error('Location search error:', error)
            cb([])
        }
    }

    return {
        queryLocationSuggestions
    }
}